package com.intellagric.service.iftmonitor.Impl.ift_equipment;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.IftEquipmentAssociateMapper;
import com.intellagric.module.iftmonitor.EquipmentVo;
import com.intellagric.pojo.IftEquipment;
import com.intellagric.pojo.IftEquipmentAssociate;
import com.intellagric.pojo.IftEquipmentAssociateExample;
import com.intellagric.service.iftmonitor.ift_equipment.EquipmentAssociateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/18
 * @DESC： 采集设备关联设备管理服务
 */
@Service
public class EquipmentAssociateServiceImpl implements EquipmentAssociateService {

    @Autowired
    private IftEquipmentAssociateMapper equipmentAssociateMapper;

    /**
     * 添加设备
     * @param equipmentAssociate
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addEquipmentAssociate(IftEquipmentAssociate equipmentAssociate) {
        if (equipmentAssociateMapper.insertSelective(equipmentAssociate) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除设备
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteEquipmentAssociate(String id) {
        if (equipmentAssociateMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改设备信息
     * @param equipmentAssociate
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editEquipmentAssociate(IftEquipmentAssociate equipmentAssociate) {
        if (equipmentAssociateMapper.updateByPrimaryKeySelective(equipmentAssociate) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 根据id查询设备
     * @param id
     * @return IftEquipmentAssociate
     */
    @Override
    public IftEquipmentAssociate getEquipmentAssociateById(String id) {
        return equipmentAssociateMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据设备id查询关联设备
     * @param equipmentId
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getEquipmentAssociateByVo(String equipmentId) {
        IftEquipmentAssociateExample equipmentAssociateExample = new IftEquipmentAssociateExample();
        equipmentAssociateExample.createCriteria().andEquipmentIdEqualTo(equipmentId);
        List<IftEquipmentAssociate> equipmentAssociateList = equipmentAssociateMapper.selectByExample(equipmentAssociateExample);
        return LayuiDataGridResult.success().add(equipmentAssociateList,equipmentAssociateList.size());
    }

    /**
     * 查询所有关联设备列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getEquipmentAssociate() {
        IftEquipmentAssociateExample equipmentAssociateExample = new IftEquipmentAssociateExample();
        equipmentAssociateExample.createCriteria().andIdIsNotNull();
        List<IftEquipmentAssociate> equipmentAssociateList = equipmentAssociateMapper.selectByExample(equipmentAssociateExample);
        return LayuiDataGridResult.success().add(equipmentAssociateList,equipmentAssociateList.size());
    }

    /**
     * 分页查询
     * @param page
     * @param rows
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getEquipmentAssociate(int page, int rows) {
        PageHelper.startPage(page,rows);
        IftEquipmentAssociateExample equipmentAssociateExample = new IftEquipmentAssociateExample();
        equipmentAssociateExample.createCriteria().andIdIsNotNull();
        List<IftEquipmentAssociate> equipmentAssociateList = equipmentAssociateMapper.selectByExample(equipmentAssociateExample);
        PageInfo<IftEquipmentAssociate> pageInfo = new PageInfo<>(equipmentAssociateList);
        return LayuiDataGridResult.success().add(equipmentAssociateList,pageInfo.getTotal());
    }
}
